﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using fcwms.Common;

//public partial class admin_user_user_list : System.Web.UI.Page
public partial class admin_user_user_list : SessionManage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ChkAdminLevel("sys_user", ActionEnum.View.ToString()); //检查权限
            fcwms.Model.dt_users model = GetAdminInfo();
            RptBind();
            roleBind(model.role_type);
            //branchBind(ddlBranch);
            branchBind2();
        }
        //if (!Page.IsPostBack)
        //{
        //    //ChkAdminLevel("sys_manager", ActionEnum.View.ToString()); //检查权限
        //    ////取得管理员信息
        //    //Model.manager model = GetAdminInfo();
        //    //RptBind("role_type>=" + model.role_type + CombSqlTxt(this.keywords), "add_time desc");
        //}
    }

    #region 角色部门绑定==========================
    public void roleBind(int role_type)
    {
        fcwms.BLL.dt_role bll = new fcwms.BLL.dt_role();
        DataTable dt = bll.GetList("").Tables[0];
        this.selRoleName.Items.Clear();
        this.selRoleName.Items.Add(new ListItem("请选择角色...", ""));
        foreach (DataRow dr in dt.Rows)
        {
            if (Convert.ToInt32(dr["role_type"]) >= role_type)
            {
                this.selRoleName.Items.Add(new ListItem(dr["role_name"].ToString(), dr["role_name"].ToString()));
            }
        }
        ////将数据捆绑到下拉列表中
        //StringBuilder strSql = new StringBuilder();
        //strSql.Append("select role_name from dt_role ");
        //DataSet ds = DbHelperSQL.Query(strSql.ToString());
        //ddlRoleName.DataSource = ds.Tables[0];
        //ddlRoleName.DataTextField = "role_name"; //设置列表显示的字段 文本内容
        //ddlRoleName.DataValueField = "role_name";//设置列表显示的字段对应的值
        //ddlRoleName.DataBind();
        //ddlRoleName.Items.Insert(0, new ListItem("请选择角色...", ""));//第一项中加入内容,重点是绑定后添加 请选择一级名称
    }
    //public void branchBind(DropDownList ddl)
    //{
    //    //ddlBranch.Attributes.Add("size ", "8");
    //    fcwms.BLL.dt_branch bll = new fcwms.BLL.dt_branch();
    //    DataTable dt = bll.GetList("").Tables[0];
    //    ddlBranch.Items.Clear();
    //    ddlBranch.Items.Add(new ListItem("请选择部门...", ""));
    //    foreach (DataRow dr in dt.Rows)
    //    {
    //        ddl.Items.Add(new ListItem(dr["branch_name"].ToString(), dr["id"].ToString()));
    //    }



    //    ////将数据捆绑到下拉列表中
    //    //StringBuilder strSql = new StringBuilder();
    //    //strSql.Append("select branch_name from dt_branch order by id  ");
    //    //DataSet ds = DbHelperSQL.Query(strSql.ToString());
    //    //ddlBranch.DataSource = ds.Tables[0];
    //    //ddlBranch.DataTextField = "branch_name"; //设置列表显示的字段 文本内容
    //    //ddlBranch.DataValueField = "branch_id";//设置列表显示的字段对应的值
    //    //ddlBranch.DataBind();
    //    //ddlBranch.Items.Insert(0, new ListItem("请选择部门...", ""));//第一项中加入内容,重点是绑定后添加 请选择一级名称
    //}
    public void branchBind2()
    {      
        fcwms.BLL.dt_branch bll = new fcwms.BLL.dt_branch();
        DataTable dt = bll.GetList("").Tables[0];
        this.selBranch.Items.Clear();
        this.selBranch.Items.Add(new ListItem("请选择部门...", ""));      
        //this.selBranch.Value = "请选择部门...";
        foreach (DataRow dr in dt.Rows)
        {
            this.selBranch.Items.Add(new ListItem(dr["branch_name"].ToString(), dr["id"].ToString()));
        }
           
        //fcwms.BLL.dt_branch bll = new fcwms.BLL.dt_branch();
        //DataTable dt = bll.GetList("").Tables[0];
        //this.sel.DataSource = dt.DefaultView;//取出所有项作为select的数据源
        //this.sel.DataValueField = "id";//绑定每一项的值
        //this.sel.DataTextField = "branch_name";//绑定每一项的显示文本
        //this.sel.DataBind();
        //this.sel.Items.Insert(0, new ListItem ("请选择部门...", ""));//第一项中加入内容,重点是绑定后添加 请选择一级名称
        //this.sel.Value = 100;//在上面绑定的N多项中设置你当前想要显示哪一项

        ////将数据捆绑到下拉列表中
        //StringBuilder strSql = new StringBuilder();
        //strSql.Append("select branch_name from dt_branch order by id  ");
        //DataSet ds = DbHelperSQL.Query(strSql.ToString());
        //ddlBranch.DataSource = ds.Tables[0];
        //ddlBranch.DataTextField = "branch_name"; //设置列表显示的字段 文本内容
        //ddlBranch.DataValueField = "branch_id";//设置列表显示的字段对应的值
        //ddlBranch.DataBind();
        //ddlBranch.Items.Insert(0, new ListItem("请选择部门...", ""));//第一项中加入内容,重点是绑定后添加 请选择一级名称
    }
    #endregion

    #region 数据绑定==============================
    public void RptBind()
    {
        //string role_name = ddlRoleName.SelectedValue;
        string role_name = this.selRoleName.Value;
        string user_id = txtUserID.Text.Trim();
        string user_name = txtUserName.Text.Trim();
        //string branch_id = ddlBranch.SelectedValue;
        //string[] branch_id2 = Request.Form.GetValues("sel");
        //string branch_id = branch_id2[sel.SelectedIndex ];
        string branch_id = this.selBranch.Value;
        string userid = Session["username"].ToString();
        StringBuilder strSel = new StringBuilder();
        strSel.Append("select role_type from dt_users where user_id=@user_id ");
        SqlParameter[] parms_role_type = new SqlParameter[] { 
        new SqlParameter("@user_id",userid)};
        int role_type = Convert.ToInt32(DbHelperSQL.GetSingle(strSel.ToString(), parms_role_type));

        StringBuilder strSql = new StringBuilder();
        strSql.Append("select dt_role.role_name,dt_users.* ");
        strSql.Append("from dt_users left outer join dt_role on (dt_role.id = dt_users.role_id) where 1=1  ");
        //strSql.Append("from dt_users,dt_role ");
        //strSql.Append("select * from dt_users ");

        //strSql.Append("where 1=1 ");
        //strSql.Append("where dt_role.id = dt_users.role_id ");
        if (this.selRoleName.Value.Trim() != string.Empty)
        {
            strSql.Append("and role_name=@role_name ");
        }
        if (txtUserID.Text.Trim() != string.Empty)
        {
            strSql.Append("and user_id=@user_id ");
        }
        if (txtUserName.Text != string.Empty)
        {
            strSql.Append("and user_name like '%' + @user_name + '%' ");
        }
        //if (ddlBranch.Text.Trim() != string.Empty)
        if (this.selBranch.Value.Trim() != string.Empty)
        {
            strSql.Append("and branch_id=@branch_id ");
        }
        //if (ddlLock.Text.Trim() == "正常")
        if (this.selLock.Value.Trim() == "正常")
        {
            strSql.Append("and is_lock=0 ");
        }
        if (this.selLock.Value.Trim() == "禁用")
        {
            strSql.Append("and is_lock=1 ");
        }
        strSql.Append("and dt_users.role_type >=@role_type  ");
        //strSql.Append("and user_id != 'admin'");
        strSql.Append("order by id desc ");

        SqlParameter[] parms = new SqlParameter[] {
            new SqlParameter("@role_name",role_name),
            new SqlParameter("@user_id",user_id),
            new SqlParameter("@user_name",user_name),
            new SqlParameter("@role_type",role_type),
            new SqlParameter("@branch_id",branch_id)};
        DataSet ds = DbHelperSQL.Query(strSql.ToString(), parms);
        this.AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;  //DataView的Count属性,AspNetPager的RecordCount属性.
        PagedDataSource pds = new PagedDataSource();
        pds.AllowPaging = true;                                   //启用分页.
        pds.PageSize = AspNetPager1.PageSize;                     //每页要显示的记录条数.    
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1; //当前页索引.
        pds.DataSource = ds.Tables[0].DefaultView;                //分页数据源以DataView为数据源.     
        lblCount.Text = AspNetPager1.RecordCount.ToString();      //总共几条数据
        //Label1.Text = AspNetPager1.PageSize.ToString();
        rptList.DataSource = pds;
        rptList.DataBind();
    }

    ////设置分页数量
    //protected void ddlNum_SelectedIndexChanged1(object sender, EventArgs e)
    //{
    //    AspNetPager1.CurrentPageIndex = 1;
    //    AspNetPager1.PageSize = Convert.ToInt32(ddlNum.Text.Trim());
    //    RptBind();
    //}
    protected void btn10_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        AspNetPager1.PageSize = 1;
        RptBind();
    }
    protected void btn20_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        AspNetPager1.PageSize = 20;
        RptBind();
    }
    protected void btn50_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        AspNetPager1.PageSize = 50;
        RptBind();
    }

    //分页绑定
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        RptBind();
    }
    ////设置分页数量
    //protected void txtPageNum_TextChanged(object sender, EventArgs e)
    //{
    //    Page.Validate();
    //    if (Page.IsValid)
    //    {
    //        AspNetPager1.CurrentPageIndex = 1;
    //        AspNetPager1.PageSize = Convert.ToInt32(txtPageNum.Text.Trim());
    //        RptBind();
    //    }
    //}
    #endregion

    #region 查询操作==============================
    
    protected void lbtnSearch_Click(object sender, EventArgs e)//查询
    {
        AspNetPager1.CurrentPageIndex = 1;
        RptBind();
    }

    //清空条件
    protected void lbtnReset_Click(object sender, EventArgs e)
    {
        txtUserID.Text = string.Empty;
        txtUserName.Text = string.Empty;
        //ddlBranch.Text = string.Empty;
        this.selBranch.Value = string.Empty;
        //ddlRoleName.Text = string.Empty;
        this.selRoleName.Value = string.Empty;
        this.selLock.Value = "";
        RptBind();
    } 
    #endregion

    #region 删除操作==============================
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ChkAdminLevel("sys_user", ActionEnum.Delete.ToString()); //检查权限
        fcwms.BLL.dt_users bll = new fcwms.BLL.dt_users();
        for (int i = 0; i < rptList.Items.Count; i++)
        {
            int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
            CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
            if (cb.Checked)
            {
                bll.Delete(id);
            }
        }
        MessageBox.Jscript(this, "批量删除成功啦", "user_list.aspx", "Success");
    }
    #endregion
}